package com.kaikeba.dao.impl;

import com.kaikeba.dao.BaseADminDao;
import com.kaikeba.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-07-28 16:30
 * @Modified By:
 */
public class AdminDaoMysql implements BaseADminDao {
    private static final String SQL_UPDATE_LOGIN_TIME = "UPDATE EADMIN SET LOGINTIME=?,LOGINIP=? WHERE USERNAME=? ";
    private static final String SQL_LOGIN = "SELECT ID FROM EADMIN WHERE USERNAME=? AND PASSWORD=?";

    /*
     * 根据用户名，更新登录时间，与ip
     * */
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        //1.获取连接
        Connection conn = DruidUtil.getConnection();
        //2.获取预对象
        PreparedStatement pre = null;
        try {
            pre = conn.prepareStatement(SQL_UPDATE_LOGIN_TIME);
            pre.setObject(1,new java.sql.Date(date.getTime()));
            pre.setObject(2,ip);
            pre.setObject(3,username);
            pre.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,pre,null);
        }
    }
    /*
     * 管理员根据账号密码登录
     * */
    @Override
    public boolean login(String username, String password) {
        //1.获取连接
        Connection conn = DruidUtil.getConnection();
        //2.获取预对象
        PreparedStatement pre = null;
        ResultSet res = null;
        try {
            pre = conn.prepareStatement(SQL_LOGIN);
            pre.setObject(1,username);
            pre.setObject(2,password);

            res = pre.executeQuery();
            return res.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(conn,pre,res);
        }
        return false;
    }
}
